package com.pig4cloud.pig.module.controller.mer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.Mer;
import com.pig4cloud.pig.module.api.entity.mer.MerPrinter;
import com.pig4cloud.pig.module.api.entity.mer.MerPrinterRelation;
import com.pig4cloud.pig.module.api.entity.product.ProductType;
import com.pig4cloud.pig.module.service.mer.IMerPrinterRelationService;
import com.pig4cloud.pig.module.service.mer.IMerPrinterService;
import com.pig4cloud.pig.module.service.mer.IMerService;
import com.pig4cloud.pig.module.service.product.IProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商家打印机和商品分类的关联Controller
 *
 * @author ruoyi
 * @date 2023-07-17
 */
@Validated
@Api(value = "商家打印机和商品分类的关联控制器", tags = {"商家打印机和商品分类的关联管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/relation")
public class MerPrinterRelationController {

    private final IMerPrinterRelationService merPrinterRelationService;
    private final IMerService merService;
    private final IMerPrinterService merPrinterService;
    private final IProductTypeService productTypeService;

    /**
     * 查询商家打印机和商品分类的关联列表
     */
    @ApiOperation("查询商家打印机和商品分类的关联列表")
    @PreAuthorize("@pms.hasPermission('module:relation:list')")
    @GetMapping("/list")
    public TableDataInfo<MerPrinterRelation> list(@Validated MerPrinterRelation merPrinterRelation) {
        QueryWrapper<MerPrinterRelation> qw = new QueryWrapper<>();
            if (merPrinterRelation.getMerId()!=null){
                qw.eq("mer_id",merPrinterRelation.getMerId());
            }
            if(merPrinterRelation.getPrinterId()!=null){
                qw.eq("printer_id",merPrinterRelation.getPrinterId());
            }
        TableDataInfo<MerPrinterRelation> td = PageUtils.buildDataInfo(merPrinterRelationService.page(PageUtils.buildPage(),qw));
        List<MerPrinterRelation> list = td.getData();
        for (MerPrinterRelation list1:list){
            if (list1.getMerId()!=null){
                Mer mer = merService.getById(list1.getMerId());
                if (mer!=null){
                    list1.setMerName(mer.getShopName());
                }
            }
            if (list1.getPrinterId()!=null){
                MerPrinter merPrinter = merPrinterService.getById(list1.getPrinterId());
                if (merPrinter!=null){
                    list1.setPrinterName(merPrinter.getClientAlias());
                }
            }
            if (list1.getCateId()!=null){
                ProductType productType = productTypeService.getById(list1.getCateId());
                if (productType!=null){
                    list1.setCateName(productType.getTypeName());
                }
            }
        }
        return td;
    }

    /**
     * 获取商家打印机和商品分类的关联详细信息
     */
    @ApiOperation("获取商家打印机和商品分类的关联详细信息")
    @PreAuthorize("@pms.hasPermission('module:relation:query')")
    @GetMapping("/{id}")
    public R<MerPrinterRelation> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        MerPrinterRelation merPrinterRelation = merPrinterRelationService.getById(id);
        return R.ok(merPrinterRelation);
    }

    /**
     * 新增商家打印机和商品分类的关联
     */
    @ApiOperation("新增商家打印机和商品分类的关联")
    @PreAuthorize("@pms.hasPermission('module:relation:add')")
    @SysLog("商家打印机和商品分类的关联")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody MerPrinterRelation merPrinterRelation) {
        if (merPrinterRelation.getCateId()!=null){
            ProductType productType = productTypeService.getById(merPrinterRelation.getCateId());
            if (productType!=null){
                merPrinterRelation.setDishType(Long.valueOf(productType.getDishType()));
            }
        }
        return R.okByRow(merPrinterRelationService.save(merPrinterRelation) ? 1 : 0);
    }

    /**
     * 修改商家打印机和商品分类的关联
     */
    @ApiOperation("修改商家打印机和商品分类的关联")
    @PreAuthorize("@pms.hasPermission('module:relation:edit')")
    @SysLog("商家打印机和商品分类的关联")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody MerPrinterRelation merPrinterRelation) {
        if (merPrinterRelation.getCateId()!=null){
            ProductType productType = productTypeService.getById(merPrinterRelation.getCateId());
            if (productType!=null){
                merPrinterRelation.setDishType(Long.valueOf(productType.getDishType()));
            }
        }
        return R.okByRow(merPrinterRelationService.updateById(merPrinterRelation) ? 1 : 0);
    }

    /**
     * 删除商家打印机和商品分类的关联
     */
    @ApiOperation("删除商家打印机和商品分类的关联")
    @PreAuthorize("@pms.hasPermission('module:relation:remove')")
    @SysLog("商家打印机和商品分类的关联")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(merPrinterRelationService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
